package org.ipower.examsystem.dao.impl;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.ipower.examsystem.dao.TeachingSectionsDao;
import org.ipower.examsystem.domain.DataField;
import org.ipower.examsystem.domain.TeachingSections;

/**
 * 教材章节数据访问实现类。
 * @author jeasonyoung
 */
public class TeachingSectionsDaoImpl extends ModuleGenericDaoImpl<TeachingSections> implements TeachingSectionsDao {
    /**
     * 获取不包括指定教材章节ID子目录所有子章节集合.
     * @param sectionID 章节ID。
     * @return 子章节集合。
     * */
    @Override
    public List<DataField> NotSelfSections(String sectionID) {
        List<DataField> result = new ArrayList<DataField>();
        if(sectionID != null && !sectionID.isEmpty()){
            List<Object> list = this.namedQuery("getNotSelfSections", new String[]{"FieldValue"},  new Object[]{sectionID});
            if(list != null && list.size() > 0){
                for(int i = 0; i < list.size(); i++){
                     Object obj = list.get(i);
                     if(obj != null && (obj instanceof DataField)){
                         result.add((DataField)obj);
                     }
                }
            }
        }
        return result;
    }   

    @Override
    public List<TeachingSections> bindAllData(String catalogID) {
        if(catalogID != null && !catalogID.isEmpty()){
           List<Object> list =  this.namedQuery("tobindAllData", new String[]{"catalogID"}, new Object[]{catalogID});
           if(list != null && list.size() > 0){
               List<TeachingSections> result = new ArrayList<TeachingSections>();
               for(int i = 0; i < list.size(); i++){
                   Object obj = list.get(i);
                   if(obj != null && (obj instanceof DataField)){
                       DataField data = (DataField)obj;
                       TeachingSections ts = new TeachingSections();
                       ts.setSectionID(data.getFieldID());
                       ts.setSectionName(data.getFieldName());
                       ts.setFullSectionName(data.getFullFieldName());
                        result.add(ts);
                   }
               }
               return result;
           }
        }
         return null;
    }
}